<?php
/*----------------------------------------------------------------------
 * 公用函数模型管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Common\Logic;

class CommonLogic{
	
	//按层级，获取地区数据
	public function getAreaOptions($params) {
		$result = array();
		$model = M();
		$select_sql = '';
		if(array_key_exists('parentid', $params)) {
			$select_sql .= ' AND parentid = "'.$params['parentid'].'"';
		}
		$sql = 'SELECT area_id,name,parentid 
				FROM mt_area 
				WHERE 1'.$select_sql;
		$rs = $model->query($sql);
		foreach($rs as $key => $value) {
			$result[$value['area_id']] = $value['name'];
		}
		return $result;
	}
	
	
	/*
	*批量获取代理商授权房间数
	*param   $id = int;
	*return  int
	*/
	public function getAgentRoomNum($agentid){
		$data = I('request.');
		$temp['agentid'] = $data['params'];
		$info = M('agent')->where($temp)->find();
		
		$map['agent_id'] = $info['id'];
		$map['is_online'] = 1;
		$cinema = M('cinema_host')->field('buy_hall_count')->where($map)->select();
		foreach($cinema as $k => $v){
			foreach($v as $k1 => $v1){
				$arr[] = $v1;
			}
		}
		$num = array_sum($arr);
		return $num;
		
	}
	
	/**
     * 通过接口，远程调用影院方法
     * @param  array
     * @return array
     * @author ming.nie
     */
	public function getRemoteCinemaMethod($params)
	{
		$result = array();
        $result_manager = array(
            0 => array('status_code'=>0,'status_message'=>'获取影院信息失败'),
            1 => array('status_code'=>1,'status_message'=>'获取影院信息成功'),
            3 => array('status_code'=>3,'status_message'=>'参数错误'),
        );
        $status_code = 0;
        if(array_key_exists('host_name', $params)) {
            $status_code = 1;
            //获取影院信息
            $cinemaHostModel = M('CinemaHost');
            $condition = array();
            $condition['host_name'] = $params['host_name'];
            $cinema_info = $cinemaHostModel->where($condition)->find();
            if(array_key_exists('host_ip', $cinema_info)
               && $cinema_info['host_ip']) {
                //请求指定影院设置
                $url = 'http://'.$cinema_info['host_ip'].'/Home/Interface';
                //echo $url.'<br>';
                $method_params = $params['method_params'];
                $post_data = array();
                $post_data['class'] = $params['class'];
                $post_data['method'] = $params['method'];
                $post_data['params'] = json_encode($method_params);
                $interface_params['post_data'] = $post_data;
                $interface_params['method'] = 'post';
                $res = getNetContents($url, $interface_params);
                $res = json_decode($res, true);
                if(is_array($res)
                   && 0 == $res['res']) {
                    $result = $res['data'];
                }
            }
        } else {
            $status_code = 3;
        }
        
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}

    /**
     * 通过接口，远程获取本机图片链接
     * @param $params['picpath'] 图片路径
     * @param $params['size'] 固定大小：100x100; 比例裁图：100:100
     * @param $params['upload_ip'] 上传ip，在本地上传图片的时候传入该值，如果上传成功，远程要过来同步图片
     * @return array
     */
	public function getRemoteThumbUrl($params){
        $result = array();
        $result_manager = array(
            0 => array('status_code'=>0,'status_message'=>'获取图片信息失败'),
            1 => array('status_code'=>1,'status_message'=>'获取图片信息成功'),
            3 => array('status_code'=>3,'status_message'=>'参数错误'),
        );
        $status_code = 0;
        if($params['picpath'] && $params['size']){
            $result['picpath'] = getThumb($params['picpath'], $params['size']);
            $status_code = 1;
        }else{
            $status_code = 3;
        }
        if($params['upload_ip']){
            getRemoteImg($params['upload_ip'].$params['picpath'], $_SERVER['DOCUMENT_ROOT'].'/Uploads/logo/');
            $result['picpath'] = $params['picpath'];
            $status_code = 1;
        }
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
    }

    /**
     * 通过接口，生成本地多个图片链接
     * @param $params['picpath'] 图片路径
     * @param $params['size'] 固定大小：100x100; 比例裁图：100:100
     * @param $params['upload_ip'] 上传ip，在本地上传图片的时候传入该值，如果上传成功，远程要过来同步图片
     * @return string
     */
    public function setRemoteThumbs($params){
        $result = array();
        $result_manager = array(
            0 => array('status_code'=>0,'status_message'=>'获取图片信息失败'),
            1 => array('status_code'=>1,'status_message'=>'获取图片信息成功'),
            3 => array('status_code'=>3,'status_message'=>'参数错误'),
        );
        $picsize_arr = unserialize($params['picsize_arr']);
        if($params['picpath'] && is_array($picsize_arr)){
            foreach($picsize_arr as $k=>$size){
                $result[$size] = getThumb($params['picpath'], $size);
                $status_code = 1;
            }
        }else{
            $status_code = 3;
        }
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
    }
	
	/**
     * 扫描试用中的用户，如果试用到期，就更新状态为非试用，且清空试用时间
     * @param  array
     * @return array
     * @author ming.nie
     */
	public function clearTryCinemaHosts($params)
	{
		echo '<pre>';
		header('Content-Type:text/html; charset=utf-8;');
		
		$current_time = time();
		
		$model = M();
		$sql = 'SELECT id,host_name,is_try,try_day_max,FROM_UNIXTIME(try_day_max, "%Y-%m-%d %H:%k:%s") AS try_day_max_show FROM mt_cinema_host WHERE is_try = 1 AND try_day_max < '.$current_time;
		$rs = $model->query($sql);
		print_r($rs);
		
		$cinemaHostModel = M('CinemaHost');
		foreach($rs as $key => $value) {
			
			//更新试用状态
			$condition = array();
			$condition['id'] = $value['id'];
			$update_data = array();
			$update_data['is_try'] = 2;
			$update_data['try_day_max'] = 0;
			$cinemaHostModel->data($update_data)->where($condition)->save();
			echo $cinemaHostModel->getLastSql().'<br>';
		}
	}
	
	/**
	 * 视频播放信息更新到云端，用于版权系统统计等业务
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function syncCopyrightVideoPlay($params)
	{
        $result = array();
        $result_manager = array(
            0 => array('status_code'=>0, 'status_message'=>'数据更新失败'),
            1 => array('status_code'=>1, 'status_message'=>'数据更新成功'),
            3 => array('status_code'=>3, 'status_message'=>'参数错误'),
        );
        $status_code = 0;
        if(isset($params['host_name']) && !empty($params['host_name'])
		   && isset($params['data_list']) && !empty($params['data_list'])) {
			$model = M('CopyrightOrder');
			$copyrightVidelModel = M('CopyrightVideo');
			
            $current_time = time();
			$update_ids = array();
			$status_code = 1;
			
            foreach($params['data_list'] as $key => $value) {
                $condition = array();
                $condition['host_name'] = $params['host_name'];
                $condition['video_id'] = $value['video_id'];
				$condition['cinema_play_id'] = $value['id'];
                $check_rs = $model->where($condition)->find();
                if(!$check_rs) {
					$value['is_update'] = 1;
					$value['cinema_play_id'] = $value['id'];
					unset($value['id']);
					
					//获取当前的影片价格
					$value['price'] = $copyrightVidelModel->where('video_id='.$value['video_id'])->getField('price');
					!$value['price'] && $value['price'] = 0;
					
                    $db_result = $model->data($value)->add();
					if($db_result) {
						$update_ids[] = $value['cinema_play_id'];
					}
                }
            }
			
			if($update_ids) {
				$result['update_ids'] = $update_ids;
			}
			
        } else {
            $status_code = 3;
        }
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}

	//查询影院授权凭证：niem
	public function checkAuthInfo($params)
	{
		$result = array();
		$result_manager = array(
			0 => array('status_code'=>0,'status_message'=>'查询失败'),
			1 => array('status_code'=>1,'status_message'=>'查询成功'),
			3 => array('status_code'=>3,'status_message'=>'参数错误'),
		);
		$status_code = 0;
		if(isset($params['auth_code_1']) && !empty($params['auth_code_1'])
			&& isset($params['auth_code_2']) && !empty($params['auth_code_2'])) {
			$model = M('CinemaHost');

			$condition = array();
			$condition['auth_code_1'] = $params['auth_code_1'];
			$condition['auth_code_2'] = $params['auth_code_2'];
			if($params['id']!=''){
				$condition['id']=array('NEQ',$params['id']);
			}
			$check_auth = $model->where($condition)->find();
			if($check_auth) {
				$status_code = 1;
				$result['auth_pic'] = C('IMG_HOST_NET').$check_auth['auth_pic'];
				$result['auth_type'] = 1;
			} else {
				$agentModel = M('Agent');
				$condition = array();
				$condition['auth_code_1'] = $params['auth_code_1'];
				$condition['auth_code_2'] = $params['auth_code_2'];
				$check_auth = $agentModel->where($condition)->find();
				if($check_auth) {
					$status_code = 1;
					$result['auth_pic'] = C('IMG_HOST_NET').$check_auth['auth_pic'];
					$result['auth_type'] = 2;
				}
			}
		} else {
			$status_code = 3;
		}
		$result = array_merge($result,$result_manager[$status_code]);
		return $result;
	}

    /**
     * 同步院线信息到上报系统
     */
	public function syncTheatreList($params){
        $Theatre=D('Theatre');
        $result=array();
        $data=$Theatre->getTheatreList($params);
        if($data){
            $ids_request=array_column($params,'id');
            $ids_response=array_column($data,'id');
            $new_ids=array_diff($ids_response,$ids_request);
            foreach($data as $val){
                if(in_array($val['id'],$new_ids)){
                    $result['new'][]=$val;
                    continue;
                }
                foreach($params as $v){
                    if($val['id']==$v['id']){
                        if($val['update_time']!=$v['time']){
                            $result['update'][]=$val;
                        }
                    }
                }
            }
        }
        return $result;

    }
    public function getCinemaList($params){
        $result=array();
	    $Cinema=M('cinemaHost');
	    $Room=D('Common/cinemaRoom');
        $client_code=$params['client_code'];
        $page=$params['page'];
        if($client_code=='*'){
            $where['_string']='1=1';
        }else if($client_code){
            $where['client_code']=$client_code;
        }else{
            return $result;
        }
        $ids=D('Theatre')->where($where)->getField('id',true);
        if($ids){
            $where1['theatre_id']=array('in',$ids);
            $count=$Cinema->where($where1)->count('id');
            $result['count'] = $count;
            $field='id,cinema_code,cinema_name,room_count';
            $data=$Cinema->where($where1)->limit(($page-1)*10,10)->select();
            var_dump($data);exit;
            foreach($data as $val){
                $where3['cinema_id']=$val['id'];
                //$data['rooms']=$Room->where($where3)->select();
            }
            $result['cinemas']=$data;
        }

        return $result;
    }
}
